//
//  StationMonirotViewController.m
//  Runone
//
//  Created by runone02 on 16/11/29.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "StationMonirotViewController.h"
#import "RequestHelper.h"
#import "RequestOutginseng.h"
#import "OneConst.h"
#import "DeviceBaseInfo.h"
#import "MJExtension.h"
#import "UIImageView+WebCache.h"
#import "CameraDetailController.h"
#import "AppLocalManager.h"

@interface StationMonirotViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableView;

@property (strong, nonatomic) NSArray *dataArray; //该收费站对应的全部摄像枪的数组
@property (strong, nonatomic) NSMutableArray *squareArray;   //广场摄像枪数组
@property (strong, nonatomic) NSMutableArray *tingKongArray; //亭岗摄像枪数组
@property (strong, nonatomic) NSMutableArray *othersArray;   //其他数组

@end

@implementation StationMonirotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    label.text          = @"收费站监控列表";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor     = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    
    [self configArray];
    
    self.view.backgroundColor=[UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _tableView                 = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64) style:UITableViewStylePlain];
    _tableView.bounces         = NO;
    _tableView.scrollEnabled   = YES;
    _tableView.separatorColor  = [UIColor blackColor];
    _tableView.backgroundColor = [UIColor colorWithRed:247.0 / 255 green:247.0 / 255 blue:247.0 / 255 alpha:1];
    _tableView.delegate        = self;
    _tableView.dataSource      = self;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tableView];
    
    [self requsetData];
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
}

- (void) configArray {
    _squareArray   = [[NSMutableArray alloc] init];
    _tingKongArray = [[NSMutableArray alloc] init];
    _othersArray   = [[NSMutableArray alloc] init];
}

- (void) requsetData {
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getHighwayModel = [[RequestGinsengModel alloc] init];
    getHighwayModel.URL                  = GetDeviceDataURL;
    getHighwayModel.DataValue            = [self DataValueStr:_stationM.TollStationUID];
    getHighwayModel.UpdatedTime          = @"2000-1-1 12:00:00";
    getHighwayModel.SystemCode           = DefaultSystemCode;
    getHighwayModel.MethodName           = @"GetTollStationDeviceInfo";
    requestHelper.block = ^(RequestOutginseng *requestOutginseng,id dataValueModel){
        _dataArray = [DeviceBaseInfo mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
        [self classsifyData];
        [_tableView reloadData];
    };
    [requestHelper RequestGinsengModel:getHighwayModel andPushController:self];
}

- (void) classsifyData {
    [_squareArray removeAllObjects];
    [_tingKongArray removeAllObjects];
    [_othersArray removeAllObjects];
    for (DeviceBaseInfo *device in _dataArray) {
        if (device.DevicePositionType == 1) {  //广场
            [_squareArray addObject:device];
        }
        if (device.DevicePositionType == 2) {  //亭岗
            [_tingKongArray addObject:device];
        }
        if (device.DevicePositionType == 3) {  //其他
            [_othersArray addObject:device];
        }
    }
}


-(NSString *)DataValueStr:(NSString *)TollStationUID  {
    NSString *typeStr = [NSString stringWithFormat:@"%ld",(unsigned long)DevicesTypeCamera];
    NSString *valueStr =[NSString stringWithFormat:@"[{\"key\":\"TollStationUID\",\"value\":\"%@\"},{\"key\":\"DeviceType\",\"value\":\"%@\"}]",TollStationUID,typeStr];
    return valueStr;
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger inter = 1;
    if (_squareArray.count > 0) {
        inter = inter + 1;
    }
    if (_tingKongArray.count > 0 ) {
        inter = inter + 1;
    }
    if (_othersArray.count > 0) {
        inter = inter + 1;
    }
    return inter;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger numOfRow = 0;
    if (section == 0) {
        numOfRow = _squareArray.count;
    }
    if (section == 1) {
        numOfRow =  _tingKongArray.count;
    }
    if (section == 2) {
        numOfRow =  _othersArray.count;
    }
    return numOfRow;
    //    return _dataArray.count;
}
//_RoadPicUrl	__NSCFString *	@"http://120.76.152.148:8090/CentralResourceServer\\HighwayResource\\RoadPicture\\GuangleHighway\\广乐高速.png"	0x00006000001aa100
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cell";
    NSString *s=@"Bearer ";
    NSString *strone;
    SDWebImageDownloader *sdmanager = [SDWebImageManager sharedManager].imageDownloader;
    if (GETACCESS_TOKEN!=nil) {
        // NSLog(@"%@",GETACCESS_TOKEN);
        strone=[s stringByAppendingString:GETACCESS_TOKEN];
        [sdmanager setValue:strone forHTTPHeaderField:@"Authorization"];
    }

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
        
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.section == 0 ) {
        DeviceBaseInfo *deviceBaseInfo = _squareArray[indexPath.row];
        cell.textLabel.text  = [NSString stringWithFormat:@"%@",deviceBaseInfo.DeviceName];
        //        cell.detailTextLabel.text       =  deviceBaseInfo.DirectionName;
    }
    if (indexPath.section == 1) {
        DeviceBaseInfo *deviceBaseInfo = _tingKongArray[indexPath.row];
        cell.textLabel.text  = [NSString stringWithFormat:@"%@",deviceBaseInfo.DeviceName];
        //        cell.detailTextLabel.text       =  deviceBaseInfo.DirectionName;
    }
    if (indexPath.section == 2) {
        DeviceBaseInfo *deviceBaseInfo = _othersArray[indexPath.row];
        cell.textLabel.text  = [NSString stringWithFormat:@"%@",deviceBaseInfo.DeviceName];
        //        cell.detailTextLabel.text       =  deviceBaseInfo.DirectionName;
    }
    
    cell.imageView.autoresizingMask = UIViewAutoresizingNone;
    NSString *str = _currentRoadModel.RoadPicUrl;
    //    NSURL *urlStr = [NSURL URLWithString:_currentRoadModel.RoadPicUrl];
    //    NSLog(@"%@",urlStr);
//    NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)str,NULL,NULL,kCFStringEncodingUTF8));
//    NSURL *url = [NSURL URLWithString:str];
    [cell.imageView sd_setImageWithURL:[str mj_url]];
    CGSize itemSize = CGSizeMake(44, 44);
    UIGraphicsBeginImageContextWithOptions(itemSize, NO, 0.0);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [cell.imageView.image drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CameraDetailController *vc     = [[CameraDetailController alloc] init];
    switch (indexPath.section) {
        case 0: {
            DeviceBaseInfo *deviceBaseInfo = _squareArray[indexPath.row];
            deviceBaseInfo.RoadUID         = _currentRoadModel.RoadUID;
            vc.deviceBaseInfo             = deviceBaseInfo;
            break;
        }
        case 1: {
            DeviceBaseInfo *deviceBaseInfo = _tingKongArray[indexPath.row];
            deviceBaseInfo.RoadUID         = _currentRoadModel.RoadUID;
            vc.deviceBaseInfo             = deviceBaseInfo;
            break;
        }
        case 2: {
            DeviceBaseInfo *deviceBaseInfo = _othersArray[indexPath.row];
            deviceBaseInfo.RoadUID         = _currentRoadModel.RoadUID;
            vc.deviceBaseInfo              = deviceBaseInfo;
            break;
        }
        default:
            break;
    }
    
    //    CameraDetailController *vc     = [[CameraDetailController alloc] init];
    //    DeviceBaseInfo *deviceBaseInfo = _dataArray[indexPath.row];
    //    deviceBaseInfo.RoadUID         = _currentRoadModel.RoadUID;
    //    vc.deviceBaseInfo              = deviceBaseInfo;
    vc.currentRoadModel            = _currentRoadModel;
    vc.fromStationName               = _stationM.TollStationName;
    //        vc.roadType                    = _roadType;
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *sectionStr;
    if (section == 0 && _squareArray.count > 0) {
        sectionStr = @"广场摄像枪";
    }
    if (section == 1 && _tingKongArray.count > 0) {
        sectionStr = @"亭岗摄像枪";
    }
    if (section == 2 && _othersArray.count > 0) {
        sectionStr = @"其他摄像枪";
    }
    return sectionStr;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0 && _squareArray.count > 0) {
        return 25;
    }
    if (section == 1 && _tingKongArray.count > 0) {
        return 25;
    }
    if (section == 2 && _othersArray.count > 0) {
        return 25;
    }
    return 0.001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
    
}




@end
